package org.dey.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.mapper.ExerciseMapper;
import org.dey.pojo.CheckUpload;
import org.dey.pojo.Exercise;
import org.dey.pojo.PageVo;
import org.dey.service.ExerciseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class ExerciseServiceimpl extends ServiceImpl<ExerciseMapper, Exercise> implements ExerciseService {
    @Autowired
    ExerciseMapper exerciseMapper;

    @Override
    public PageVo<Exercise> queryExercise(Exercise exercise, int pageNum, int pageSize) {
        //组装条件
        QueryWrapper<Exercise> queryWrapper = new QueryWrapper<Exercise>();

        //根据名字模糊查询
        if(StringUtils.isNotBlank(exercise.getProjectname())) {
            queryWrapper.like("projectname", exercise.getProjectname());
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Exercise> exercises = this.list(queryWrapper);

        PageInfo<Exercise> pageInfo = new PageInfo<Exercise>(exercises);
        PageVo<Exercise> pageVo =new PageVo<Exercise>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());
        return pageVo;
    }

    @Override
    public int insertExercise( Exercise exercise) {
        int num = 0;

        QueryWrapper<Exercise> queryWrapper = new QueryWrapper<Exercise>();
        queryWrapper.eq("Projectname", exercise.getProjectname());

        Exercise one = this.getOne(queryWrapper);

        if(one==null){
            boolean save = this.save(exercise);
            if(save){
                num=1;
            }
        }
        return num;
    }

    @Override
    public int deleteExercise(int id) {
        int num = 0;
        boolean b = this.removeById(id);
        if(b){
            num = 1;
        }
        return num;
    }

    @Override
    public Exercise queryExerciseByid(int id) {
        return this.getById(id);
    }

    @Override
    public int updateExercise(Exercise exercise) {
        int num = 0;
        boolean b = this.updateById(exercise);
        if(b){
            num = 1;
        }
        return num;
    }
}
